package com.codechen.scaffold.news.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.codechen.scaffold.admin.domain.entity.SysUser;
import com.codechen.scaffold.admin.domain.request.SysUserQueryRequest;
import com.codechen.scaffold.common.domain.Result;
import com.codechen.scaffold.news.domain.entity.News;
import com.codechen.scaffold.news.domain.request.NewsQueryRequest;
import com.codechen.scaffold.news.domain.request.NewsSaveRequest;
import com.codechen.scaffold.news.domain.vo.NewsBaseVO;
import com.codechen.scaffold.news.domain.vo.NewsVO;
import com.codechen.scaffold.news.service.INewsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author : wuweihong
 * @desc : TODO  请填写你的功能描述
 * @date : 2025-06-15
 */

@RestController
@RequestMapping("/news")
@Api(tags = "新闻管理")
public class NewsController {

	@Autowired
	private INewsService newsService;

	@PostMapping("/")
	@ApiOperation("保存新闻")
	public Result<Void> saveNews(@RequestBody NewsSaveRequest saveRequest) {
		newsService.saveNews(saveRequest, "draft");
		return Result.success(null);
	}

	@PostMapping("/publish")
	@ApiOperation("发布新闻")
	public Result<Void> publishNews(@RequestBody NewsSaveRequest saveRequest) {
		newsService.saveNews(saveRequest, "published");
		return Result.success(null);
	}

	@ApiOperation(value = "新闻列表")
	@PostMapping("/pageList/{pageNo}/{pageSize}")
	public Result list(@PathVariable(value = "pageNo") Long pageNo,
	                   @PathVariable(value = "pageSize") Long pageSize,
	                   @RequestBody NewsQueryRequest newsQueryRequest) {
		Page<NewsBaseVO> page = new Page<>(pageNo, pageSize);
		IPage<NewsBaseVO> newsBaseVOIPage = newsService.queryList(page, newsQueryRequest);
		return Result.success(newsBaseVOIPage);
	}

	@ApiOperation(value = "新闻详情")
	@GetMapping("/detail/{id}")
	public Result<NewsVO> getById(@PathVariable("id") Long id) {
		return Result.success(newsService.findNewsById(id));
	}

	@ApiOperation(value = "删除")
	@GetMapping("/del/{id}")
	public Result<Void> del(@PathVariable("id") Long id) {
		newsService.removeNews(id);
		return Result.success(null);
	}

}
